package service;

import com.alibaba.fastjson.JSON;
import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.Activity;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;
import org.junit.Test;
import org.junit.jupiter.api.DisplayName;
import org.openqa.selenium.remote.DesiredCapabilities;
import utils.WaitUtils;

import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

@DisplayName("美团新开城市")
public class GetMTNewCity {
    @Test
    public void get() throws MalformedURLException, InterruptedException {

        DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
        desiredCapabilities.setCapability("platformName", "Android");
        desiredCapabilities.setCapability("platformVersion", "7");
        desiredCapabilities.setCapability("deviceName", "xx");
        desiredCapabilities.setCapability("automationName","uiautomator2");
//        desiredCapabilities.setCapability("appPackage", "com.ygkj.chelaile.standard");
//        desiredCapabilities.setCapability("appActivity", "dev.xesam.chelaile.app.module.func.SplashActivity");
        desiredCapabilities.setCapability("noReset", true);
        desiredCapabilities.setCapability("newCommandTimeout ",0);
        desiredCapabilities.setCapability("desiredCapabilities", true);//可以输入中文
        //  desiredCapabilities.setCapability("app","D:\\my-mayun\\apk\\chelaile_app_chelaile_www_lastest.apk");
        //  desiredCapabilities.setCapability("autoGrantPermissions",true);//自动同意一些授权
        URL remoteUrl = new URL("http://127.0.0.1:4723/wd/hub");
        AndroidDriver driver = new AndroidDriver(remoteUrl, desiredCapabilities);
        Activity a = new Activity("com.sankuai.meituan", "com.meituan.android.pt.homepage.activity.MainActivity");
        driver.startActivity(a);
        //得到所有城市
        List<String> allCityList = getAllCity(driver);

        System.out.println(JSON.toJSONString(allCityList));
        //点击选择城市
        MobileElement el3 = (MobileElement) driver.findElementById("com.sankuai.meituan:id/city_name");
        el3.click();
        List<MobileElement> cityList = driver.findElementsByXPath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.support.v4.view.ViewPager/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.ListView/android.widget.LinearLayout");
        WaitUtils.sleep(1);

        MobileElement city = cityList.get(5).findElementById("com.sankuai.meituan:id/citylist_textview");
        String cityname = city.getText();
        WaitUtils.sleep(1);
        city.click();
        try {
            List<MobileElement> findBusList = driver.findElementsById("com.sankuai.meituan:id/util_area_text");
            boolean isSupportBus = false;
            for (MobileElement findBus : findBusList) {
                if (findBus.getText().equals("查公交")) {
                    isSupportBus = true;
                    break;
                }
                isSupportBus = false;
            }
            if (isSupportBus) {
                System.out.println(cityname + " 支持");
            } else {
                System.out.println(cityname + " 不支持");
            }

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(cityname + " 不支持查公交");
        }


    }

    private List<String> getAllCity(AndroidDriver driver) {
        List<String> allCityList = new ArrayList<>();
        //点击选择城市
        MobileElement el3 = (MobileElement) driver.findElementById("com.sankuai.meituan:id/city_name");
        el3.click();
        int i = 0;
        while (true) {
            boolean isAdd = false;
            List<MobileElement> cityList;
            try{
                cityList=  driver.findElementsByXPath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.support.v4.view.ViewPager/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.ListView/android.widget.LinearLayout");
            }catch (Exception e){
                e.printStackTrace();
                WaitUtils.sleep(1);
                cityList=  driver.findElementsByXPath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.support.v4.view.ViewPager/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.ListView/android.widget.LinearLayout");
            }

            for (MobileElement cityElement : cityList) {
                MobileElement city;
                try {
                    city = cityElement.findElementById("com.sankuai.meituan:id/citylist_textview");
                } catch (Exception e) {
                   // e.printStackTrace();
                    continue;
                }
                String cityname = city.getText();
                if (!allCityList.contains(cityname)) {
                    allCityList.add(cityname);
                    isAdd = true;
                }
            }
            try {
                (new TouchAction(driver)).press(PointOption.point(407, 1898)).waitAction(WaitOptions.waitOptions(Duration.ofSeconds(2)))
                        .moveTo(PointOption.point(407, 1000)).release().perform();
            }catch (Exception e){
               // e.printStackTrace();
                continue;
            }
            if (!isAdd) {
                i++;
            }
            if (i > 3) {
                break;
            }

        }
        return allCityList;
    }
}
